"use strict";
const electron = require("electron");
const CutImagePreload = {
  //   截图
  openScreenShot: (type) => electron.ipcRenderer.send("open_screen_shot", type),
  showScreenShot: () => electron.ipcRenderer.send("show_screen_shot"),
  getScreenSouce: (fn) => {
    electron.ipcRenderer.on("get_screen_source", (_event, source) => fn(source));
  },
  confirmSceenShot: (data) => {
    electron.ipcRenderer.send("confirm_screen_shot", data);
  },
  closeScreenShot: () => electron.ipcRenderer.send("close_screen_shot"),
  getCutScreenResult: (fn) => {
    electron.ipcRenderer.on("cut_screen_result", (_event, data) => fn(data));
  },
  getScreenSize: () => {
    return {
      width: window.screen.width,
      height: window.screen.height
    };
  }
};
const UserPreload = {
  // 连接ws 合并数据
  connectWs: (params) => {
    return electron.ipcRenderer.invoke("connectWs", params);
  },
  login: () => {
    electron.ipcRenderer.send("login");
  },
  // 注册
  register: (params) => {
    electron.ipcRenderer.send("register", params);
  },
  // 推出登录
  logout: () => {
    electron.ipcRenderer.send("logout");
  },
  // 关闭ws
  closeWs: () => {
    electron.ipcRenderer.send("closeWs");
  }
};
const WindowPreload = {
  // 窗口控制
  controlTabBar: (params) => {
    electron.ipcRenderer.send("controlTabBar", params);
  },
  // 创建窗口
  createWindow: (params) => {
    return electron.ipcRenderer.invoke("createWindow", params);
  },
  // 创建子窗口
  createChildWindow: (params) => {
    electron.ipcRenderer.send("createChildWindow", params);
  },
  // 关闭窗口
  closePage: (name, isBackLogin = false) => {
    electron.ipcRenderer.send("closePage", name, isBackLogin);
  }
};
const SessionPreload = {
  //  更新session
  updateSessionByField: (sessionId, ownerId, data) => {
    return electron.ipcRenderer.invoke("getLocalSession", sessionId, ownerId, data);
  },
  // 获取本地session
  getLocalSession: (userId) => {
    return electron.ipcRenderer.invoke("getLocalSession", userId);
  },
  // 存储本地session
  storeLocalSession: (session) => {
    return electron.ipcRenderer.invoke("storeLocalSession", session);
  },
  // 置顶 session
  togglePinSession: (params) => {
    return electron.ipcRenderer.invoke("togglePinSession", params);
  },
  // 删除 sesion
  softDeleteSession: (sessionId, ownerId) => {
    return electron.ipcRenderer.invoke("softDeleteSession", sessionId, ownerId);
  },
  // 添加session
  addSession: (session) => {
    return electron.ipcRenderer.invoke("addSession", session);
  },
  // 处理未读消息
  handelUnread: (sessionId, ownerId) => {
    return electron.ipcRenderer.invoke("handelUnread", sessionId, ownerId);
  },
  // 获取本地message
  getLocalMessage: (sessionId, ownerId) => {
    return electron.ipcRenderer.invoke("getLocalMessage", sessionId, ownerId);
  }
};
const FilePreload = {
  getDefaultDownLoadPath: () => {
    return electron.ipcRenderer.invoke("getDefaultDownLoadPath");
  },
  // 打开文件选择
  openFileByPath: (filePath) => {
    return electron.ipcRenderer.send("openFileByPath", filePath);
  },
  // 另存为
  saveFileAs: (sourceFilePath) => {
    return electron.ipcRenderer.invoke("saveFileAs", sourceFilePath);
  },
  // 复制文件
  copyFileByPath: (filePath) => {
    return electron.ipcRenderer.invoke("copyFileByPath", filePath);
  },
  // 打开文件所在目录
  openFileDirectoryByPath: (filePath) => {
    return electron.ipcRenderer.invoke("openFileDirectoryByPath", filePath);
  },
  // 下载文件
  downloadFile: (params) => {
    electron.ipcRenderer.send("downloadFile", params);
  },
  // 下载进度
  downloadProgress: (callBack) => {
    electron.ipcRenderer.on("downloadProgress", (_event, data) => callBack(data));
  },
  // 下载完成
  downloadDone: (callBack) => {
    electron.ipcRenderer.on("downloadDone", (_event, data) => callBack(data));
  },
  // 暂停下载
  pauseDownload: (taskId) => {
    electron.ipcRenderer.send("pauseDownload", taskId);
  },
  // 恢复下载
  resumeDownload: (taskId) => {
    electron.ipcRenderer.send("resumeDownload", taskId);
  },
  // 取消下载
  cancelDownload: (taskId) => {
    electron.ipcRenderer.send("cancelDownload", taskId);
  }
};
const MessagePreload = {
  getMessage: (callback) => {
    electron.ipcRenderer.on("getMessage", (_event, value) => callback(value));
  },
  updateLocalMessage: (callback) => {
    electron.ipcRenderer.on("updateLocalMessage", (_event, value) => callback(value));
  },
  sendMessage: (data) => {
    electron.ipcRenderer.send("sendMessage", data);
  },
  // 获取本地message
  getChatMessage: (sessionId, ownerId, pageInfo) => {
    return electron.ipcRenderer.invoke("getChatMessage", sessionId, ownerId, pageInfo);
  },
  // 删除单个消息
  deleteMessageByOne: (sessionId, ownerId) => {
    return electron.ipcRenderer.invoke("deleteMessageByOne", sessionId, ownerId);
  },
  // 删除全部消息
  deleteMessageByAll: (userId, friendId, chatType) => {
    return electron.ipcRenderer.invoke("deleteMessageByAll", userId, friendId, chatType);
  },
  // 查询所有文件
  findAllMessageFile: (userId, fileName) => {
    return electron.ipcRenderer.invoke("findAllMessageFile", userId, fileName);
  },
  // 获取自己的文件
  getMessageFileByOnwer: (userId) => {
    return electron.ipcRenderer.invoke("getMessageFileByOnwer", userId);
  },
  // 获取好友聊天文件
  getMessageFileByFriend: (userId, sessionId) => {
    return electron.ipcRenderer.invoke("getMessageFileByFriend", userId, sessionId);
  },
  // 获取群聊天文件
  getMessageFileByGroup: (userId, sessionId) => {
    return electron.ipcRenderer.invoke("getMessageFileByGroup", userId, sessionId);
  },
  // 更新文件本地路径
  updateMessageFilePath: (userId, sessionId, localPath) => {
    return electron.ipcRenderer.invoke("updateMessageFilePath", userId, sessionId, localPath);
  }
};
electron.contextBridge.exposeInMainWorld("electronApi", {
  ...UserPreload,
  ...MessagePreload,
  ...SessionPreload,
  ...FilePreload,
  ...CutImagePreload,
  ...WindowPreload
});
